package pers.supeijin.devtools.converter.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FilePathUtils {
	private static List<String> getFileStings(String str) {
		ArrayList<String> strings = new ArrayList<>();
		String newStr = str;
		while (newStr.startsWith("../")) {
			strings.add(newStr.substring(0, 3));
			newStr = newStr.substring(3);
		}
		strings.add(newStr);
		return strings;
	}

	private static String getRelativeFilePath(String filePath, String relativePath) {
		String[] paths = filePath.split("\\/");
		List<String> fileStings = getFileStings(relativePath);
		int num = fileStings.size();
		StringBuilder sb = new StringBuilder();
		int index = paths.length - num;
		for (int i = 0, len = paths.length; i < len; i++) {
			if (i < index) {
				sb.append(paths[i]).append(File.separator);
			}
		}
		int lastIndex = fileStings.size() - 1;
		sb.append(fileStings.get(lastIndex));
		return sb.toString();
	}
	private static String getFilePathFromParentFile(File file,String folderName,String path){
		File parentFile = file.getParentFile();
		if(parentFile==null){
			return "";
		}
		File[] listFiles = parentFile.listFiles();
		for (int i = 0,len=listFiles.length; i <len ; i++) {
			String name = listFiles[i].getName();
			if(Objects.equals(name,folderName)){
				String filePath = parentFile.getPath()+File.separator+path;
				File targetFile = new File(filePath);
				if(targetFile.exists()){
					return targetFile.getPath();
				}
				break;
			}
		}
		return getFilePathFromParentFile(parentFile,folderName,path);
	}
	public static String getTargetFilePath(String currentFilePath, String targetFileName) {
		if (targetFileName != null && targetFileName != null) {
			if (targetFileName.startsWith("./")) {
				return currentFilePath.substring(0, currentFilePath.lastIndexOf("/")) + targetFileName.substring(1);
			}else if( targetFileName.indexOf("/")<0){
				return currentFilePath.substring(0, currentFilePath.lastIndexOf("/")) + "/"+targetFileName;
			} else if (targetFileName.startsWith("../")) {
				return getRelativeFilePath(currentFilePath, targetFileName);
			} else if (targetFileName.startsWith("/")) {
				//绝对路径支持绝对路径
				File file = new File(currentFilePath);
				String substring = targetFileName.substring(1);
				String folderName = substring.substring(0, substring.indexOf("/"));
				String filePathFromParentFile = getFilePathFromParentFile(file, folderName, targetFileName);
				return filePathFromParentFile;
			}
		}
		return "";
	}
	/**
	 * 从导入语句文本获取文件路径;
	 * @param text 导入语句文本
	 * @return 文件路径
	 */
	public static String getFilePathFromImportText(String text) {
		if (text != null) {
			Pattern p = Pattern.compile("(\"(.*?)\")|(\'(.*?)\')");
			Matcher matcher = p.matcher(text);
			if (matcher.find()) {
				return matcher.group();
			}
		}
		return "";
	}
	/**
	 *
	 * @param path 路径
	 * @return
	 */
	public static String getFileNameFromPath(String path){
		if(path!=null){
			String pathText = removeQuotationMarks(path);
			int indexOf = pathText.lastIndexOf("/");
			if(indexOf>=0){
				return pathText.substring(indexOf+1);
			}
		}
		return "";
	}

	/**
	 * 移除引号（单引号，双引号）
	 * @param text
	 * @return
	 */
	public static String removeQuotationMarks(String text){
		if(text!=null){
			return text.replaceAll("\"|\'","");
		}
		return "";
	}
}
